package com.kx.smsplantform.strategy.impl;

import com.kx.smsplantform.client.CacheClient;
import com.kx.smsplantform.strategy.Strategy;
import com.kx.smsplantform.utils.CheckMobileOperator;
import com.kx.smsplatform.common.constants.CacheConstants;
import com.kx.smsplatform.common.constants.OperatorConstants;
import com.kx.smsplatform.common.enums.InterfaceExceptionEnums;
import com.kx.smsplatform.common.exception.SmsException;
import com.kx.smsplatform.common.model.StandardSubmit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Set;

/**
 * 号段补全处理策略
 *
 * @Author Sun
 * @Date 2021/7/2 11:36
 */
@Slf4j
@Service(value = "phase")
public class PhaseStrategy implements Strategy {
    @Autowired
    private CacheClient cacheClient;

    @Override
    public boolean strategy(StandardSubmit standardSubmit) {
        log.info("【策略模块-号段补全校验】 号段补全校验策略开始执行！！");

        //1. 通过正则表达式得到手机号的运营商.
        String destMobile = standardSubmit.getDestMobile();
        //运营商 0 - 未知,1 - 移动, 2 - 联通 ,3 - 电信
        if (CheckMobileOperator.isChinaMobilePhoneNum(destMobile)) {
            //移动
            standardSubmit.setOperatorId(OperatorConstants.CHINA_MOBILE);
        } else if (CheckMobileOperator.isChinaUnicomPhoneNum(destMobile)) {
            //联通
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNICOM);
        } else if (CheckMobileOperator.isChinaTelecomPhoneNum(destMobile)) {
            //电信
            standardSubmit.setOperatorId(OperatorConstants.CHINA_TELECOM);
        } else {
            //未知
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNKNOWN);
        }

        //2. 通过Redis缓存中的phase,对比手机号的前7位,获取到手机号的归属地
        String provinceAndCity = cacheClient.get(CacheConstants.CACHE_PREFIX_PHASE + destMobile.substring(0, 7));
        if (provinceAndCity == null ) {
            // TODO 出问题了.....
            throw new SmsException(InterfaceExceptionEnums.INTERFACE_MOBILE_BELONGIN_ERROR);
        }
        String[] pac = provinceAndCity.split(",");
        //封装省和市
        standardSubmit.setProvinceId(Integer.parseInt(pac[0]));
        standardSubmit.setCityId(Integer.parseInt(pac[1]));
        //3. 获取当前客户绑定的所有通道(Channel),再根据手机号的运营商选择一个通道,如果没有合适的,选择全网通
        Set<Map> clientChannels = cacheClient.smembersMap(CacheConstants.CACHE_PREFIX_ROUTER + standardSubmit.getClientID());

        for (Map clientChannel : clientChannels) {
            if((Integer)clientChannel.get("channelType") == 0){
                // 全网通,直接配置上全网通信息
                this.setData(standardSubmit,clientChannel);
                continue;
            }
            if (standardSubmit.getOperatorId() == clientChannel.get("channelType")) {
                // 选择对了指定的通道信息
                this.setData(standardSubmit,clientChannel);
                break;
            }
        }

        log.info("【策略模块-号段补齐】 号段补齐策略执行结束！！");
        return true;
    }


    // 封装数据
    private void setData(StandardSubmit submit,Map clientChannel){
        //4. 通过通道获取下发源号码(106932423423)
        submit.setSrcNumber(clientChannel.get("spNumber") + "" + clientChannel.get("extendNumber"));
        //5. 通过通道获得当前短信费用
        submit.setFee(Long.parseLong(clientChannel.get("price") + ""));
        //6. 获取通道id,设置到网关ID中
        submit.setGatewayID((Integer)clientChannel.get("channelId"));
    }
}

